package com.kcs.fxmargin.action;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.kcs.common.utils.Constant;
import com.kcs.common.utils.commonUtils;
import com.kcs.fxmargin.action.form.ValidateSettlementActoinForm;
import com.kcs.fxmargin.bo.ValidateSettlementBO;
import com.kcs.fxmargin.model.TDeals;
import com.kcs.fxmargin.service.DealsService;

public class ValidateSettlementActoin extends FXAction {
	private Logger log = Logger.getLogger(Constant.LOG_FX);

	DealsService service = new DealsService();

	@Override
	public ActionForward process(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// TODO Auto-generated method stub
		try{

			log.debug("ValidateSettlementActoin : Start");
			
			ValidateSettlementActoinForm formBean = (ValidateSettlementActoinForm) form;
			
			if("init".equals(formBean.getAction())){
				formBean.resetExpect();
				formBean.setDate(commonUtils.formatDate(commonUtils.getCurrentDate(), "dd/MM/yyyy"));
				formBean.setValidateSettlementBOList(new ArrayList<ValidateSettlementBO>());
				
			}else if("search".equals(formBean.getAction())) search(formBean, request);
			
			formBean.setAction("init");
			
			log.debug("ValidateSettlementActoin : End");
			
		}catch(Exception e){
			log.error(e.getMessage());
			return (mapping.findForward("error"));
		}
		
		
		return mapping.findForward("validateSettlement");
	}
	
	public void search(ValidateSettlementActoinForm formBean, HttpServletRequest request) throws Exception {
		log.debug("ValidateSettlementActoin : search : Start");
		
		TDeals deals = new TDeals();
		deals.setCifNo(formBean.getCifNo());
		deals.setValueDate(commonUtils.convertStringtoSQLDate(formBean.getDate(), "dd/MM/yyyy"));
		
		formBean.setValidateSettlementBOList(service.search(deals));
		request.setAttribute(Constant.SHOW_RESULT, Constant.SHOW_RESULT);
		
		if(formBean.getValidateSettlementBOList() != null && formBean.getValidateSettlementBOList().size() > 0){
			request.setAttribute(Constant.NOT_EMPTY, Constant.NOT_EMPTY);
		}
		
		log.debug("ValidateSettlementActoin : search : End");
	}

}
